{extend name="base/base" /}
{block name="main"}
<form id="form" method="post" class="layui-form layui-form-pane" action="{:url('/admin/edit')}">
    <div class="layui-form-item">
        <label class="layui-form-label">账号</label>
        <div class="layui-input-block">
            <input type="text" name="user" placeholder="请输入" autocomplete="off" class="layui-input"
                   value="{:isset($list.user)?$list.user:''}" {if condition="isset($list['uid'])" }readonly {else /}
            lay-verify="required|username"{/if}>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">昵称</label>
        <div class="layui-input-block">
            <input type="text" name="name" placeholder="请输入" autocomplete="off" class="layui-input"
                   value="{:isset($list.name)?$list.name:''}" lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        {if condition="!isset($list['uid'])"}
            <div class="layui-input-block">
                <input type="password" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input" value=""
                       lay-verify="required|password">
            </div>
        {else /}
            <div class="layui-input-block">
                <input type="password" name="password" placeholder="重置密码，不填不修改" autocomplete="off" class="layui-input" value=""
                       lay-verify="password">
            </div>
        {/if}

    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">所属组</label>
        <div class="layui-input-block">
            {form:select name="group_id" list="$grouplist" value = "$list['group_id']"  default="[''=>'请选择']"  option="['xm-select'=>'selectId','id'=>'group_id','lay-verify'=>'required']"}
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">是否禁用</label>
        <div class="layui-input-block">
            {form:radio name="status" list="['0'=>'是','1'=>'否']" value="$list['status']"}

        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <input type="hidden" name="uid" value="{:isset($list['uid'])?$list['uid']:''}">
            <input type="hidden" name="__token__" id="token" value="{$Request.token}"/>
            <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
{/block}
{block name="script"}
{css href="/form-select/formSelects-v4.css" /}
<script>
    layui.config({
        base: '/form-select/' //此处路径请自行处理, 可以使用绝对路径
    }).extend({
        formSelects: 'formSelects-v4'
    });
    layui.use(['form', 'jquery', 'formSelects'], function () {
        var formSelects = layui.formSelects, form = layui.form, $ = layui.jquery;
        formSelects.on('selectId', function (id, vals, val, isAdd, isDisabled) {
            $('#group_id').val(layui.formSelects.value(id, 'val'));
        }, true);
        //表单验证
        form.verify({
            username: function (value, item) { //value：表单的值、item：表单的DOM对象
                var msg;
                $.ajax({
                    async: false,
                    url: "{:url('/admin/check')}",
                    data: {username: value},
                    success: function (data) {
                        if (data.code) {
                            msg = data.msg;
                        }
                    }
                });
                return msg;
            },
            password:function(value, item){
                if(value){
                    if(!(/^[\w\W]{6,25}$/.test(value))){
                        return '用户名不能全为数字';
                    }
                }
            }
        });
        //表单提交
        form.on("submit(*)", function (data) {
            var obj = $(this);
            obj.attr("disabled", "disabled").addClass("layui-disabled");
            $.post(data.form.action, data.field, function (data) {
                var icon = 5;
                if (data.code) {
                    icon = 6;
                }
                layer.msg(data.msg, {icon: icon, time: 1500}, function () {   //提示的插件，可以使用alert代替
                    if (data.code) {
                        setTimeout(function () {
                            //刷新父页面
                            parent.location.reload();
                        }, 500);
                    } else {
                        $('#token').val(data.data.token);
                        obj.removeAttr("disabled").removeClass("layui-disabled");
                    }
                });
            }, 'json');
            return false;
        });
    });


</script>
{/block}